/*
 * Records page specific API
 */

function fnGetMemRecordsRestUrl(id, cmd, args) {
	const prefix = fnGetProductRestPrefix();
	let url =  `${prefix}records/${id}/${cmd}`;

	if (args) {
        const query = new URLSearchParams(args).toString();
		url = `${url}?${query}`;
	}
	return url;
}

function fnEvtShowProcessesDetails(id, type, region_type) {
	createDialogWithTable("processes_by_name_dialog");

    const args = {};
    if (type === "processes_by_type_name") {
        args["region_type"] = region_type;
    }

	const url = fnGetMemRecordsRestUrl(id, type, args);
	$("#processes_by_name_table").dataTable( {
		"bProcessing": true,
		"sAjaxSource": url,
		"bDestroy": true,
		"sScrollX": "1000px",
		"sScrollY": "300px",
		"columnDefs": [{
			visible: false,
			targets: [0, 1]
		}],
		"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
		    const regionTypesLink = `<a href="javascript:fnEvtShowRegionTypesTables('${aData[0]}','regiontypes_by_process')">${aData[9]}</a>`;
			$('td:eq(7)', nRow).html(regionTypesLink);

            const regionDetailsLink = `<a href="javascript:fnEvtShowRegionDetailsTables('${aData[0]}','regions_by_process')">${aData[10]}</a>`;
			$('td:eq(8)', nRow).html(regionDetailsLink);
			return nRow;
		}
	} );

	$('#download_xml_processes').off('click').on('click', function () {
	    args["format"] = "xml";
		window.open(fnGetMemRecordsRestUrl(id, type, args));
	});
	$("#processes_by_name_dialog").appendTo("body").modal("show");
}

function fnEvtShowProcessesTables(id) {
	createDialogWithTable("processes_dialog");

	const url = fnGetMemRecordsRestUrl(id, "processes", {});
	$("#processes_table").dataTable( {
		"bProcessing": true,
		"sAjaxSource": url,
		"bDestroy": true,
		"sScrollX": "1000px",
		"sScrollY": "300px",
		"columnDefs": [{
			visible: false,
			targets: [0, 1, 2]
		}],
		"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
			const processDetailsLink = `<a href="javascript:fnEvtShowProcessesDetails(${aData[0]}, 'processes_by_name')">${aData[5]}</a>`;
			$('td:eq(2)', nRow).html(processDetailsLink);

			const regionTypesLink = `<a href="javascript:fnEvtShowRegionTypesTables(${aData[0]}, 'regiontypes_by_process')">${aData[9]}</a>`;
			$('td:eq(6)', nRow).html(regionTypesLink);

            const regionDetailsLink = `<a href="javascript:fnEvtShowRegionDetailsTables(${aData[0]}, 'regions_by_process')">${aData[10]}</a>`;
			$('td:eq(7)', nRow).html(regionDetailsLink);
			return nRow;
		}
	} );

	$('#download_xml_processes').off('click').on('click', function () {
		window.open(fnGetMemRecordsRestUrl(id, "processes", {"format":"xml"}));
	});
	$("#processes_dialog").appendTo("body").modal("show");
}

function fnEvtShowRegionDetailsTables(id, type, name) {
	createDialogWithTable("region_details_dialog");

    const args = {};
    if (name) {
        args["name"] = name;
    }

	const url = fnGetMemRecordsRestUrl(id, type, args);
	$("#region_details_table").dataTable( {
		"bProcessing": true,
		"sAjaxSource": url,
		"bDestroy": true,
		"sScrollX": "1000px",
		"sScrollY": "300px"
	} );

	$('#download_xml_region_details').off('click').on('click', function () {
	    args["format"] = "xml";
		window.open(fnGetMemRecordsRestUrl(id, type, args));
	});
	$("#region_details_dialog").appendTo("body").modal("show");
}

function fnEvtShowRegionTypesTables(id, type) {
	createDialogWithTable("region_types_dialog");

	const url = fnGetMemRecordsRestUrl(id, type, {});
	const hidden = (type === "regiontypes_by_process") ? [0, 1, 2, 3] : [0, 1];

	$("#region_types_table").dataTable( {
		"bProcessing": true,
		"sAjaxSource": url,
		"bDestroy": true,
		"columnDefs": [{
			visible: false,
			targets: hidden
		}],
		"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
		    if (type === "regiontypes") {
                const procDetailsLink = `<a href="javascript:fnEvtShowProcessesDetails(${id},'processes_by_type_name','${aData[4]}')">${aData[2]}</a>`;
                $('td:eq(0)', nRow).html(procDetailsLink);

                const regionTypesLink = `<a href="javascript:fnEvtShowRegionTypesTables('${aData[4]}','regiontypes_by_name')">${aData[4]}</a>`;
                $('td:eq(2)', nRow).html(regionTypesLink);

                const regionDetailsLink = `<a href="javascript:fnEvtShowRegionDetailsTables(${id},'regions_by_type_name','${aData[4]}')">${aData[5]}</a>`;
                $('td:eq(3)', nRow).html(regionDetailsLink);
			} else if (type === "regiontypes_by_name") {
			    const procDetailsLink = `<a href="javascript:fnEvtShowProcessesDetails(${aData[1]},'processes_by_type_name','${aData[4]}')">${aData[2]}</a>`;
                $('td:eq(0)', nRow).html(procDetailsLink);

                const regionDetailsLink = `<a href="javascript:fnEvtShowRegionDetailsTables(${aData[1]},'regions_by_type_name','${aData[4]}')">${aData[5]}</a>`;
                $('td:eq(3)', nRow).html(regionDetailsLink);
			}
			else {
                const regionDetailsLink = `<a href="javascript:fnEvtShowRegionDetailsTables('${aData[0]}','regions_by_type')">${aData[5]}</a>`;
                $('td:eq(1)', nRow).html(regionDetailsLink);
			}

			return nRow;
		}
	} );

	$('#download_xml_region_types').off('click').on('click', function () {
		window.open(fnGetMemRecordsRestUrl(id, type, {"format":"xml"}));
	});
	$("#region_types_dialog").appendTo("body").modal("show");
}

function loadMemRecords() {
	$('#all_records').off('click').on('click', function () {
		window.open(fnGetAllItemsFilterUrl("records", {"format":"xml"}));
	});

	// Check if DataTable is already initialized
	if ($.fn.DataTable.isDataTable('#records')) {
		// If already initialized, just destroy and recreate
		$('#records').DataTable().destroy();
	}

	$('#records').dataTable( {
		"bProcessing": true,
		"sAjaxSource": fnGetAllItemsFilterUrl("records", {}),
		"pageLength": 15,
		"bPaginate": true,
		"bDestroy": true,
		"sAjaxDataProp": "data",
		"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
			const processesLink = `<a href="javascript:fnEvtShowProcessesTables(${aData[0]}, 'process')">${aData[2]}</a>`;
			$('td:eq(2)', nRow).html(processesLink);

			const regionTypesLink = `<a href="javascript:fnEvtShowRegionTypesTables(${aData[0]}, 'regiontypes')">${aData[4]}</a>`;
			$('td:eq(4)', nRow).html(regionTypesLink);

			return nRow;
		}
	} );
}

// Document Ready
$(document).ready(function () {
    loadMemRecords();
});